package com.ehsy.controller;

import com.ehsy.modo.R;
import com.ehsy.modo.UserVo;
import com.ehsy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author YanZhen_Li
 * @version 1.0
 * @title User
 * @description
 * @create 2024/4/2 10:41
 */

@Controller
@RequestMapping("/user")
public class UserController{
    @Resource
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginUser(
            @RequestParam String userName,
            @RequestParam String password,
            HttpServletRequest request){
        UserVo user = userService.getUserByUserNameAndPassword(userName, password);
        if (user!=null){
            request.getSession().setAttribute("user",user);
        }
         return "redirect:/tmp/index";
    }

    @RequestMapping(value = "/getLoginUser", method = RequestMethod.GET)
    @ResponseBody
    public R<UserVo> loginUser(HttpServletRequest request){
        UserVo user = (UserVo) request.getSession().getAttribute("user");
        R<UserVo> r = new R<>();
        if (user!=null){
            r.setCode(200);
            r.setMsg("success");
            user.setPassword(null);
            r.setData(user);
        }else {
            r.setCode(500);
            r.setMsg("fail");
        }
        return r;
    }
}
